package com.fh.fruit.controller.pic;

import java.util.HashMap;
import java.util.Map;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;

import org.joda.time.DateTime;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.multipart.MultipartFile;
import org.springframework.web.servlet.ModelAndView;

import com.fh.controller.base.BaseController;
import com.fh.fruit.service.goods.GoodsManager;
import com.fh.fruit.util.IDUtils;
import com.fh.fruit.util.JsonUtils;
import com.fh.fruit.util.OSSUtils;


/**
 * 图片上传处理
 * 
 * @author Administrator
 *
 */
@Controller
public class PictureController extends BaseController {

	private final static String picLocation="fruit/goods/"; 
	@Resource(name="goodsService")
	private GoodsManager goodsService;

	@RequestMapping(value="/pic/upload",method={RequestMethod.POST,RequestMethod.GET})
	@ResponseBody
	public String pictureUpload(MultipartFile uploadFile,HttpServletRequest request) {
		Map<String, Object> resultMap = new HashMap<String, Object>();
		/*Map<String, Object> result;
		String isCom = request.getParameter("isCom");
		if(StringUtils.equals(isCom, "01")){
			String end = request.getParameter("end");
			int width =Integer.parseInt(request.getParameter("width"));
			int height =Integer.parseInt(request.getParameter("height"));
			float quality = Float.parseFloat(request.getParameter("quality"));
			result = pictureService.uploadPicture(uploadFile, end, width, height, quality);
		}else{
			result = pictureService.uploadPicture(uploadFile);
		}
		//OSSClient client = new OSSClient(endpoint, accessKeyId, accessKeySecret);
		// 为了保证功能的兼容性，需要把Result转换成json格式的字符串。
		String json = JsonUtils.objectToJson(result);
		return json;*/
		if(uploadFile.isEmpty()){
			resultMap.put("error", 1);
			resultMap.put("message", "文件上传失败");
		}else{
			String imagePath = new DateTime().toString("yyyy-MM-dd");
	        String newName = IDUtils.genImageName();
	        String fileName=picLocation+imagePath+"/"+newName+".png";
			resultMap=OSSUtils.pictureUpload(fileName,uploadFile,"image/jpeg");
		}
		String json = JsonUtils.objectToJson(resultMap);
		return json;
	}
	
	@RequestMapping(value="/pic/pictureDelete",method={RequestMethod.POST,RequestMethod.GET})
	@ResponseBody
	public String pictureDelete(HttpServletRequest request) throws Exception {
		boolean result=false;
		Map<Object,String> param=new HashMap<Object, String>();
		param.put("id", request.getParameter("id"));
		String newPicture=request.getParameter("newPicture");
		String imgsrc=request.getParameter("imgsrc");
		String state=request.getParameter("state");
		param.put("newPicture", newPicture);
		param.put("imgsrc", imgsrc);
		if(newPicture!=null && request.getParameter("id")!=null && request.getParameter("id")!=""){
			result = OSSUtils.deleteFile(imgsrc);
			if(result){
				if("00".equals(state)){
					int num=goodsService.deleteImage(param);
					if(num ==0){
						return "error";
					}
				}
				if("01".equals(state)){
					int num=goodsService.updateImage(param);
					if(num ==0){
						return "error";
					}
					num=goodsService.updateSpecImage(param);
					if(num ==0){
						return "error";
					}
				}
			}
		}
		return "success";
	}
	
	
	@RequestMapping(value="/pic/picCutView")
	public ModelAndView picCutView(){
		ModelAndView mv = this.getModelAndView();	
		mv.setViewName("hltx/pic/pic_view");
		return mv;
	}
	
	
	
}